﻿// -----------------------------------------------------------------------
// <copyright file="DataContext.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.DataAccess.Contexts
{
	#region Usings

	using System.Data.Entity;

	using MBOP.Core.Entities.Common;
	using MBOP.Core.Entities.Library;

	#endregion

	/// <summary>
	/// Represent context of the application's database
	/// which can be used to query from a database.
	/// </summary>
	public class DataContext : DbContext
	{
		#region Constants

		private string TABLE_NAME_BOOKSTAGS = "BooksTags";
		private string COLUMN_NAME_BOOKID = "BookID";
		private string COLUMN_NAME_TAGID = "TagID";

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the collection of all book entities in the context.
		/// </summary>
		public DbSet<Book> Books { get; set; }

		/// <summary>
		/// Gets or sets the collection of all book subscription entities in the context.
		/// </summary>
		public DbSet<BookSubscription> BooksSubscriptions { get; set; }
		
		/// <summary>
		/// Gets or sets the collection of all image entities in the context.
		/// </summary>
		public DbSet<Image> Images { get; set; }
		
		/// <summary>
		/// Gets or sets the collection of all data entities in the context.
		/// </summary>
		public DbSet<Data> Data { get; set; }

		/// <summary>
		/// Gets or sets the collection of all tag entities in the context.
		/// </summary>
		public DbSet<Tag> Tags { get; set; }

		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
			base.OnModelCreating(modelBuilder);

			modelBuilder.Entity<Book>()
				.HasMany(book => book.Tags)
				.WithMany(tag => tag.Books)
				.Map(map =>
				{
					map.ToTable(TABLE_NAME_BOOKSTAGS);
					map.MapLeftKey(COLUMN_NAME_BOOKID);
					map.MapRightKey(COLUMN_NAME_TAGID);
				});
		}

		#endregion
	}
}
